<?php
class Communication_Model_Message{
	private $params=array();
	private $recipient;
	private $sender='zen_cms.zen';
	
	private $attachments;
	private $type;
	private $alias;
	private $language;
	
	
	
	const EMAIL='0';
	const SMS='1';
	const LOCAL='2';
	
	
	
	
	
	/**
	 * @return the $params
	 */
	public function getParams() {
		return $this->params;
	}

	/**
	 * @return the $recipient
	 */
	public function getRecipient() {
		return $this->recipient;
	}

	/**
	 * @return the $from
	 */
	public function getSender() {
		return $this->sender;
	}

	/**
	 * @return the $attachments
	 */
	public function getAttachments() {
		return $this->attachments;
	}

	/**
	 * @return the $type
	 */
	public function getType() {
		return $this->type;
	}

	/**
	 * @return the $alias
	 */
	public function getAlias() {
		return $this->alias;
	}

	/**
	 * @return the $language
	 */
	public function getLanguage() {
		return $this->language;
	}

	/**
	 * @param field_type $params
	 */
	public function setParams($params) {
		$this->params = $params;
		return $this;
	}

	/**
	 * @param field_type $recipient
	 */
	public function setRecipient($recipient) {
		$this->recipient = $recipient;
		return $this;
	}

	/**
	 * @param field_type $from
	 */
	public function setSender($sender) {
		$this->from = $sender;
		return $this;
	}

	/**
	 * @param field_type $attachments
	 */
	public function setAttachments($attachments) {
		$this->attachments = $attachments;
		return $this;
	}

	/**
	 * @param field_type $type
	 */
	public function setType($type) {
		$this->type = $type;
		return $this;
	}

	/**
	 * @param field_type $alias
	 */
	public function setAlias($alias) {
		$this->alias = $alias;
		return $this;
	}

	/**
	 * @param field_type $language
	 */
	public function setLanguage($language) {
		$this->language = $language;
		return $this;
	}
	
	
	private function getTemplate(){
			$model=new Communication_Model_Tpl();
			$tpl=$model->select()->setColumns(array('text','header'))->getOne("alias='".$this->getAlias()."'");
			if($tpl==false){
				throw new Zend_Exception("Cant find template with params alias='".$this->getAlias()."'");
				
			}else{
				return $tpl;
			}		
	}
	

	public function send($now=true){
		$type=$this->getType();
		
		switch ($type){
			case self::EMAIL: {
				$model=new Communication_Model_Email();
				$tpl=$this->getTemplate();
			
				$params=$this->getParams();
				
				$view = new Zend_View ();
				$view->setBasePath ( ROOT_PATH . "/cms/application/modules/communication/views/" );
				$view->assign($params);
				$view->assign('tpl',$tpl['text']);
				$text = $view->render ('tpl.phtml');
				
				$view->assign('tpl',$tpl['header']);
				$subject = $view->render ('tpl.phtml');
				
				$message=array(
					'from'=>$this->getSender(),
					'to'=>$this->getRecipient(),
					'text'=>$text,
					'subject'=>$subject,
				);
				if(isset($params['attachments'])){
					$message['attachments']=$params['attachments'];	
					
				}
				
				$id=$model->create($message);
				if($now){
					$model->send_messages($id);
				}
				$res=true;
				break;	
			}
			case self::SMS:{
				$model=new Message_Model_Sms();

				$tpl=$this->getTemplate();
			
				$params=$this->getParams();
				
				$view = new Zend_View ();
				$view->setBasePath ( ROOT_PATH . "/cms/application/modules/communication/views/" );
				$view->assign($params);
				$view->assign('tpl',$tpl['text']);
				$text = $view->render ('tpl.phtml');
				$recipient=$this->getRecipient();
				$recipient=preg_replace('/\D/', '', $recipient);
				$recipient="+".$recipient;
				
				//$res=$model->setRecipient($recipient)->setMessage($text)->sendMessage();
				$res=true;
				return $res; 
				break;
			}
			case self::LOCAL : {
				$model=new Message_Model_Local();
				$tpl=$this->getTemplate();
				$params=$this->getParams();
				$view = new Zend_View ();
				$view->setBasePath ( ROOT_PATH . "/cms/application/modules/communication/views/" );
				$view->assign($params);
				$view->assign('tpl',$tpl['text']);
				$view->assign('tpl',$tpl['header']);
				$model->setView($view)->setViewScript('tpl.phtml')->setTemplate($tpl);
				if ( isset( $params['client_id'] ) ) {
					$res = $model->oneSent( $params['client_id'] );
				}else{
					$res = $model->massSent();
				}
				break;
			}
			
		}
		
		
		return $res;	
	}
	
	
	
	
}

?>